package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.AdvimgDTO;
import com.ydd.dto.AdvimgPageDTO;
import com.ydd.entity.YddAdvImg;
import com.ydd.entity.YddAdvpos;
import com.ydd.mapper.YddAdvimgMapper;
import com.ydd.mapper.YddAdvposMapper;
import com.ydd.result.Result;
import com.ydd.service.YddAdvimgService;
import com.ydd.vo.PageVO;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvImg> implements YddAdvimgService {
    @Autowired
    private YddAdvposMapper advposMapper;
    @Autowired
    private YddAdvimgMapper advimgMapper;

    /**
     * 广告图分页
     *
     * @param dto
     * @return
     */
    @Override
    public Result pageList(AdvimgPageDTO dto) {
        if (dto == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        List<YddAdvImg> yddAdvImgList=new ArrayList<>();

        List<YddAdvImg> yddAdvImgs = advimgMapper.selectadvimgList(dto);
        for (YddAdvImg yddAdvImg : yddAdvImgs) {
            String advposname = advposMapper.selectById(yddAdvImg.getAdvimgpos()).getAdvposname();
            yddAdvImg.setAdvposname(advposname);
            yddAdvImgList.add(yddAdvImg);
        }
        IPage<YddAdvImg> page = new Page<>(dto.getPage(), dto.getLimit());
        page(page, null);
        PageVO vo = PageVO.builder()
                .count((int) page.getTotal())
                .data(yddAdvImgList)
                .totalPages((int) page.getPages())
                .pageSize((int) page.getSize())
                .currentPage((int) page.getCurrent())
                .build();

        return Result.success(vo);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Result selectByAdvimgId(Long id) {
        YddAdvImg byId = getById(id);
        return Result.success(byId);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Result updateAdvimg(AdvimgDTO dto) {
        YddAdvImg advimg = new YddAdvImg();
        LambdaQueryWrapper<YddAdvImg> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddAdvImg::getId, dto.getId());
        BeanUtils.copyProperties(dto, advimg);
        update(advimg, wrapper);

        return Result.success();
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public Result delAdvimg(Long id) {
        removeById(id);
        return Result.success();
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Result addAdvimg(AdvimgDTO dto) {
        YddAdvImg advimg = new YddAdvImg();
        BeanUtils.copyProperties(dto, advimg);
        save(advimg);
        return Result.success();
    }
}
